package com.hyy.dao.daoImpl;

import com.hyy.bean.Merchandise;
import com.hyy.bean.OrderProduct;
import com.hyy.dao.OrderProductDao;
import com.hyy.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderProductDaoImpl implements OrderProductDao {
	@Override
	public boolean insert(String oid,Merchandise merchandise,Long num) throws SQLException {
		Connection conn = JdbcUtil.getConnection();

		String sql = "insert into  order_product(oid,mid,num) value (?,?,?)";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1,oid);
		pstmt.setLong(2,merchandise.getId());
		pstmt.setLong(3,num);

		int n = pstmt.executeUpdate();
		return n==1?true:false;

	}

	@Override
	public List<OrderProduct> showProductByOid(String oid) throws SQLException {
		Connection conn = JdbcUtil.getConnection();

		String sql = "select mid, num from order_product where oid=?";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1,oid);

		ResultSet rs = pstmt.executeQuery();
		List<OrderProduct> orderProducts = new ArrayList<>();
		while (rs.next()){
			orderProducts.add(new OrderProduct(rs.getLong(1),rs.getLong(2)));
		}
		return orderProducts;
	}
}
